package net.exoweb.samples
{
    import flash.display.*;
    import flash.events.*;
    import flash.geom.*;
    import flash.text.*;

    import net.exoweb.animate.graphics.*;

    [SWF(width="640", height="360", backgroundColor="#000000")]
    public class GSuperEllipseTester extends Sprite
    {
        [Embed(source="exoweb.png")]
        private var exoImage:Class;
        private var exoweb:Bitmap = new exoImage();
        private var pixelmap:GPixelMap = new GPixelMap(exoweb.bitmapData);
        private var sE:GSuperEllipse = new GSuperEllipse(250, 170, 10, 0x000000, 5, 0x00A0FF, 1);
        private var dir:int = 0;
        private var dird:int = 1;
        private var dx:int = 0;
        private var rx:int = 0;

        public function GSuperEllipseTester()
        {
            sE.x = 310;
            sE.y = 180;
            sE.display();
            addChild(sE);
            pixelmap.x = 229;
            pixelmap.y = 154;
            addChild(pixelmap);
            addEventListener(Event.ENTER_FRAME, update);
            addEventListener(MouseEvent.MOUSE_DOWN, down);
        }

        public function down(e:Event):void
        {
            stage.displayState = "fullScreen";
        }

        public function reset():void
        {
            removeChild(pixelmap);
            pixelmap = new GPixelMap(exoweb.bitmapData);
            sE = new GSuperEllipse(250, 170, 10, 0x000000, 5, 0x00A0FF, 1);
            dir = 0;
            dird ^= 1;
            dx = 0;
            rx = 0;
            sE.x = 310;
            sE.y = 180;
            sE.display();
            addChild(sE);
            pixelmap.x = 229;
            pixelmap.y = 154;
            addChild(pixelmap);
        }

        public function update(e:Event):void
        {
            if(dir == 0)
            {
                if(sE.factor - 0.1 <= 0)
                {
                    dir = 1;
                    sE.factor = 0;
                }
                else
                {
                    sE.factor -= 0.1;
                }
            }
            else if(dir == 1)
            {
                sE.factor += 0.0125;
                sE.rx -= 3;
                sE.ry -= 2;
                if(sE.factor > 1)
                {
                    sE.factor = 1.0;
                    sE.rx = 10;
                    sE.ry = 10;
                    dir = 2;
                    removeChild(sE);
                    drawO(80, 18);
                }
            }
            else
            {
                if(dx < pixelmap.pixels.length)
                {
                    for(var j:int=0; j<pixelmap.pixels[dx].length; j++)
                    {
                        if(pixelmap.pixels[dx][j].color != 0)
                        {
                            pixelmap.pixels[dx][j].alpha = 1;
                            pixelmap.pixels[dx][j].display();
                        }
                    }
                    dx++;
                }
                else
                {
                    if(dird == 1)
                    {
                        rx += pixelmap.pixels[i].length;
                        for(var i:int=0; i<pixelmap.pixels.length; i++)
                        {
                            for(j=0; j<pixelmap.pixels[0].length; j++)
                            {
                                pixelmap.pixels[i][j].x += Math.random() * 5;
                                pixelmap.pixels[i][j].y += Math.random() * 5;
                                pixelmap.pixels[i][j].alpha -= 0.015;
                                if(i * pixelmap.pixels[0].length + j >= rx) break;
                            }
                            if(i * pixelmap.pixels[0].length + j >= rx) break;
                            if(pixelmap.pixels[pixelmap.pixels.length-1][pixelmap.pixels[0].length-1].y > 200)
                            {
                                reset();
                            }
                        }
                    }
                    else
                    {
                        rx += pixelmap.pixels.length;
                        for(j=0; j<pixelmap.pixels[0].length; j++)
                        {
                            for(i=0; i<pixelmap.pixels.length; i++)
                            {
                                pixelmap.pixels[i][j].x += Math.random() * 5;
                                pixelmap.pixels[i][j].y -= Math.random() * 5;
                                pixelmap.pixels[i][j].alpha -= 0.015;
                                if(j * pixelmap.pixels.length + i >= rx) break;
                            }
                            if(j * pixelmap.pixels.length + i >= rx) break;
                            if(pixelmap.pixels[pixelmap.pixels.length-1][pixelmap.pixels[0].length-1].y < 0)
                            {
                                reset();
                            }
                        }
                    }
                }
            }
        }

        public function drawO(dx:int, dy:int):void
        {
            if(pixelmap.pixels[dx][dy].alpha != 1)
            {
                if(pixelmap.pixels[dx][dy].alpha == 0)
                {
                    pixelmap.pixels[dx][dy].alpha = 1;
                    pixelmap.pixels[dx][dy].display();
                    drawO(dx-1, dy);
                    drawO(dx+1, dy);
                    drawO(dx, dy-1);
                    drawO(dx, dy+1);
                }
            }
        }
    }
}
